package com.zw.a.basic.a10.demo06_linkedlist;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

//案例: 演示LinkedList集合入门.
//概述: LinkedList集合解释: 底层数据结构是链表, 所以增删快, 查询修改慢.
public class Demo01 {
    public static void main(String[] args) {
        //1.创建LinkedList集合对象, 存储字符串数据: "hello", "world", "java"
        LinkedList<String> list =  new LinkedList<>();
        list.add("hello");
        list.add("world");
        list.add("java");

        //2.遍历LinkedList集合.
        //方式一: 普通的迭代器.
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }
        System.out.println("--------------------------------");

        //方式二: 普通for
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }
        System.out.println("--------------------------------");

        //方式三: 增强for
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("--------------------------------");

        //方式四: 列表迭代器
        ListIterator<String> lit = list.listIterator();
        while (lit.hasNext()) {
            String s = lit.next();
            System.out.println(s);
        }
        System.out.println("--------------------------------");

        //方式五: 转数组遍历
        Object[] objs = list.toArray();
        for (Object obj : objs) {
            System.out.println(obj);
        }

    }
}
